// 查
var dayjs = require("dayjs");
var express = require("express");
var router = express.Router();
var dayjs = require("dayjs");
const db = require("../disposition/db");
// 查看道具接口————————————————————————————————————————————————————————————————————————————
router.get("/Prop", (req, res) => {
  console.log("查看道具接口");
  let sql = "";
  if (req.query.state === "全部") {
    req.query.name === ""
      ? (sql = "SELECT * from props WHERE Agree=1")
      : (sql = `SELECT * from props WHERE Agree=1 AND name LIKE '%${req.query.name}%'`);
  } else {
    sql = `SELECT * from props WHERE classify="${req.query.state}" AND Agree=1`;
    req.query.name === ""
      ? (sql = `SELECT * from props WHERE classify="${req.query.state}" AND Agree=1`)
      : (sql = `SELECT * from props WHERE classify="${req.query.state}" AND Agree=1 AND name LIKE '%${req.query.name}%'`);
  }
  console.log(sql);
  db.query(sql, (err, result) => {
    if (err) {
      return;
    }
    if (result.length === 0) {
      res.send({
        msg: "暂无此类道具！",
        success: false,
      });
    } else {
      res.json({
        data: result,
        success: true,
      });
    }
  });
});
// 查看道具矩阵接口————————————————————————————————————————————————————————————————————————
router.get("/Prop/Matrix", (req, res) => {
  console.log("查看道具矩阵接口");
  const sql = "SELECT * FROM props_matrix";
  db.query(sql, (err, result) => {
    if (err) {
      return;
    }
    if (result.length === 0) {
      res.send({
        msg: "查询矩阵失败",
        success: false,
      });
    } else {
      result.some((item) => {
        item.Created_Data !== null
          ? (item.Created_Data = dayjs(item.Created_Data + 86400000).format(
              "YYYY-MM-DD"
            ))
          : "";
      });
      res.json({
        data: result,
        success: true,
      });
    }
  });
});
// 查看正式库角色接口——————————————————————————————————————————————————————————————————————
router.get("/Role", (req, res) => {
  console.log("查看正式库角色接口");
  let row = [];
  // 获取前端数据
  const query = req.query;
  let sql = "SELECT * FROM character_guide WHERE Agree=1 ORDER BY id desc";
  // 查询数据库
  if (query.name === "") {
    sql = "SELECT * FROM character_guide WHERE Agree=1 ORDER BY id desc";
  } else {
    sql = `SELECT * FROM character_guide WHERE Agree=1 AND alias LIKE '%${query.name}%' OR Agree=1 AND name LIKE '%${query.name}%' OR Agree=1 AND Japanese_name LIKE '%${query.name}%' OR Agree=1 AND alias LIKE '%${query.name}%' ORDER BY id desc `;
  }

  // 拼接筛选数据库数据的数组
  let splicing = [];
  // 判断攻击类型、攻击速度、属性、获取方式哪个是有值的哪个有数据就给拼接数组进行赋值
  query.Type !== "" ? splicing.push(`Attack_distance='${query.Type}'`) : "";
  query.Velocity !== ""
    ? splicing.push(`Attack_speed='${query.Velocity}'`)
    : "";
  query.Attribute !== "" && query.Attribute !== "多属性"
    ? splicing.push(`attribute='["${query.Attribute}"]'`)
    : "";
  query.Fetch !== "" ? splicing.push(`obtain='${query.Fetch}'`) : "";
  // 判断分类是否有被选来进行查询代码的赋值
  if (
    query.Type !== "" ||
    query.Velocity !== "" ||
    (query.Attribute !== "" && query.Attribute !== "多属性") ||
    query.Fetch !== ""
  ) {
    // 将数据库查询语句变为筛选查询
    if (query.name === "") {
      sql =
        "SELECT * from character_guide WHERE Agree=1 AND" +
        " " +
        splicing.join(" " + "AND" + " ") +
        "ORDER BY id desc";
    } else {
      sql =
        "SELECT * from character_guide WHERE Agree=1 AND" +
        " " +
        splicing.join(" " + "AND" + " ") +
        " " +
        `AND   name LIKE '%${query.name}%'` +
        " OR Agree=1 AND " +
        splicing.join(" " + "AND" + " ") +
        " " +
        `AND Japanese_name LIKE '%${query.name}%'` +
        " OR Agree=1 AND " +
        splicing.join(" " + "AND" + " ") +
        " " +
        `AND alias LIKE '%${query.name}%'`;
      ("ORDER BY id desc");
    }
  }
  // 判断要赋值哪些条件
  db.query(sql, (err, result) => {
    if (err) {
      return;
    }
    // 转化部分数组成js格式与时间矫正
    result.some((item) => {
      item.Attack_radius = JSON.parse(item.Attack_radius);
      item.Batter1 = JSON.parse(item.Batter1);
      item.Batter1_content = JSON.parse(item.Batter1_content);
      item.Batter2 = JSON.parse(item.Batter2);
      item.Batter2_content = JSON.parse(item.Batter2_content);
      item.Batter3 = JSON.parse(item.Batter3);
      item.Batter3_content = JSON.parse(item.Batter3_content);
      item.Deed_materials = JSON.parse(item.Deed_materials);
      item.arousal = JSON.parse(item.arousal);
      item.attribute = JSON.parse(item.attribute);
      item.characteristic1_content = JSON.parse(item.characteristic1_content);
      item.characteristic2_content = JSON.parse(item.characteristic2_content);
      item.task = JSON.parse(item.task);
      !item.Summons ? "" : (item.Summons = JSON.parse(item.Summons));
      item.Date_of_implementation !== null
        ? (item.Date_of_implementation = dayjs(
            item.Date_of_implementation + 86400000
          ).format("YYYY-MM-DD"))
        : "";
    });
    // 判断是否未多属性查询
    if (query.Attribute === "多属性") {
      //  找到的多属性数组数组字段
      let Result = [];
      // 循环数据库找回来的数据并判断哪些为多属性然后给暂存字段
      result.some((item) => {
        item.attribute.length > 1 ? Result.push(item) : "";
      });
      // 赋值多属性数据
      result = Result;
    }
    // 赋值合计页数
    Total = result.length;
    // 赋值非置顶数据
    row = result.splice(
      parseInt(req.query.page) * parseInt(req.query.size) -
        parseInt(req.query.size),
      parseInt(req.query.size)
    );
    res.send({
      data: row,
      Total: Total,
      success: true,
    });
  });
});
// 查看正式库角色详细接口———————————————————————————————————————————————————————————————————
router.get("/RoleDetailed", (req, res) => {
  console.log("查看正式库角色详细接口");
  // 判断id参数是否有传参
  if (req.query.id === undefined || req.query.id === "") {
    res.send({
      msg: "未知错误",
      success: false,
    });
  } else {
    // 查数据库
    const sql = `SELECT * from character_guide WHERE id="${req.query.id}"`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      // 转化部分数组成js格式与时间矫正
      result.some((item) => {
        item.Attack_radius = JSON.parse(item.Attack_radius);
        item.Batter1 = JSON.parse(item.Batter1);
        item.Batter1_content = JSON.parse(item.Batter1_content);
        item.Batter2 = JSON.parse(item.Batter2);
        item.Batter2_content = JSON.parse(item.Batter2_content);
        item.Batter3 = JSON.parse(item.Batter3);
        item.Batter3_content = JSON.parse(item.Batter3_content);
        item.Deed_materials = JSON.parse(item.Deed_materials);
        item.arousal = JSON.parse(item.arousal);
        item.attribute = JSON.parse(item.attribute);
        item.characteristic1_content = JSON.parse(item.characteristic1_content);
        item.characteristic2_content = JSON.parse(item.characteristic2_content);
        item.task = JSON.parse(item.task);
        !item.Summons ? "" : (item.Summons = JSON.parse(item.Summons));
        let GetTime = new Date(item.Date_of_implementation).getTime();
        item.Date_of_implementation = new Date(GetTime + 86400000);
      });
      if (result.length === 0) {
        res.send({
          msg: "未查找到对应数据",
          success: false,
        });
      } else {
        res.json({
          data: result[0],
          success: true,
        });
      }
    });
  }
});
// 查看正式库灯幻镜接口——————————————————————————————————————————————————————————————————————
router.get("/LampMirror", (req, res) => {
  console.log("查看正式库灯幻镜接口");
  let row = [];
  // 获取前端数据
  const query = req.query;
  // 查询数据库
  let sql = "SELECT * FROM slide_projector WHERE Agree=1 ORDER BY id desc";
  if (query.name === "") {
    sql = "SELECT * FROM slide_projector WHERE Agree=1 ORDER BY id desc";
  } else {
    sql = `SELECT * FROM slide_projector  WHERE Agree=1 AND name LIKE '%${query.name}%'  OR Agree=1 AND Japanese_name LIKE '%${query.name}%' OR Agree=1 AND alias LIKE '%${query.name}%' ORDER BY id desc `;
  }

  // 拼接筛选数据库数据的数组
  let splicing = [];
  // 判断稀有度、获取方式哪个是有值的哪个有数据就给拼接数组进行赋值
  query.Attribute !== "" ? splicing.push(`initial='${query.Attribute}'`) : "";
  query.Fetch !== "" ? splicing.push(`obtain='${query.Fetch}'`) : "";
  // 判断分类是否有被选来进行查询代码的赋值
  if (query.Attribute !== "" || query.Fetch !== "") {
    // 将数据库查询语句变为筛选查询
    sql =
      "SELECT * from slide_projector WHERE Agree=1 AND" +
      " " +
      splicing.join(" " + "AND" + " ") +
      " " +
      `AND  name LIKE '%${query.name}%'` +
      " OR " +
      "Agree=1 AND" +
      " " +
      splicing.join(" " + "AND" + " ") +
      `AND Japanese_name LIKE '%${query.name}%'` +
      " OR " +
      "Agree=1 AND" +
      " " +
      splicing.join(" " + "AND" + " ") +
      `AND alias LIKE '%${query.name}%'` +
      "ORDER BY id desc";
  }
  console.log(sql);
  // 判断要赋值哪些条件
  db.query(sql, (err, result) => {
    if (err) {
      return;
    }
    // 转化部分数组成js格式与时间矫正
    result.some((item) => {
      item.effect = JSON.parse(item.effect);
      let GetTime = new Date(item.Date_of_implementation).getTime();
      item.Date_of_implementation !== null
        ? (item.Date_of_implementation = new Date(GetTime + 86400000))
        : "";
      // item.Created_Data = item.Created_Data.split("T")[0];
    });
    // 赋值合计页数
    Total = result.length;
    // 赋值非置顶数据
    row = result.splice(
      parseInt(req.query.page) * parseInt(req.query.size) -
        parseInt(req.query.size),
      parseInt(req.query.size)
    );
    res.send({
      data: row,
      Total: Total,
      success: true,
    });
  });
});
// 查看正式库灯幻镜详细接口———————————————————————————————————————————————————————————————————
router.get("/LampMirrorIsDetailed", (req, res) => {
  console.log("查看正式库灯幻镜详细接口");
  // 判断id参数是否有传参
  if (req.query.id === undefined || req.query.id === "") {
    res.send({
      msg: "未知错误",
      success: false,
    });
  } else {
    // 查数据库
    const sql = `SELECT * from slide_projector WHERE id="${req.query.id}"`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      // 转化部分数组成js格式与时间矫正
      result.some((item) => {
        item.effect = JSON.parse(item.effect);
        let GetTime = new Date(item.Created_Data).getTime();
        item.Created_Data !== null
          ? (item.Created_Data = new Date(GetTime + 86400000))
          : "";
      });
      if (result.length === 0) {
        res.send({
          msg: "未查找到对应数据",
          success: false,
        });
      } else {
        res.json({
          data: result[0],
          success: true,
        });
      }
    });
  }
});
// 查看正式库正在进行活动接口———————————————————————————————————————————————————————————————————
router.get("/CarryOutActivities", (req, res) => {
  console.log("查看正式库正在进行活动接口");
  // 输出数据
  let output = [];
  // 查数据库
  const sql = `SELECT * from activity WHERE End_time > "${dayjs().format(
    "YYYY-MM-DD HH:mm:ss"
  )}" AND Agree=1`;
  db.query(sql, (err, result) => {
    if (err) {
      return;
    }
    // 活动转化
    result.some((item) => {
      item.Login_rewards = JSON.parse(item.Login_rewards);
      item.Bonus_character = JSON.parse(item.Bonus_character);
      item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
      item.Checkin_rewards = JSON.parse(item.Checkin_rewards);
      item.Marketplace_Redemption = JSON.parse(item.Marketplace_Redemption);
      item.Mission_rewards = JSON.parse(item.Mission_rewards);
      item.No_day_mission = JSON.parse(item.No_day_mission);
      item["is"] = "活动";
      // 活动开始时间——————————————————————————————————————————————————————————————
      if (item.Start_time !== null) {
        let Begin = new Date(item.Start_time);
        // 赋值开始时间
        item.Start_time = dayjs(item.Start_time + 86400000).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      // 活动结束时间——————————————————————————————————————————————————————
      if (item.End_time !== null) {
        // 赋值结束时间
        item.End_time = dayjs(item.End_time + 86400000).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      // 赋值输出数组
      output.push(item);
    });

    // 查数据库
    const sql2 = `SELECT * from ambush_warfare WHERE End_time > "${dayjs().format(
      "YYYY-MM-DD HH:mm:ss"
    )}" AND Agree=1`;
    db.query(sql2, (err, result) => {
      if (err) {
        return;
      }
      // 活动转化
      result.some((item) => {
        item.Login_rewards = JSON.parse(item.Login_rewards);
        item.Bonus_character = JSON.parse(item.Bonus_character);
        item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
        item.Checkin_rewards = JSON.parse(item.Checkin_rewards);
        item.Signs_of_calamity = JSON.parse(item.Signs_of_calamity);
        item.Scattered_configuration = JSON.parse(item.Scattered_configuration);
        item.Strategy = JSON.parse(item.Strategy);
        item.Marketplace_Redemption1 = JSON.parse(item.Marketplace_Redemption1);
        item.Marketplace_Redemption2 = JSON.parse(item.Marketplace_Redemption2);
        item.Mission_rewards = JSON.parse(item.Mission_rewards);
        item.No_day_mission = JSON.parse(item.No_day_mission);
        item["is"] = "调伏战";
        // 活动开始时间——————————————————————————————————————————————————————————————
        if (item.Start_time !== null) {
          let Begin = new Date(item.Start_time);
          // 赋值开始时间
          item.Start_time = dayjs(item.Start_time + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
        // 活动结束时间——————————————————————————————————————————————————————
        if (item.End_time !== null) {
          // 赋值结束时间
          item.End_time = dayjs(item.End_time + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
        // 赋值输出数组
        output.push(item);
      });

      // 查数据库
      const sql3 = `SELECT * from war_of_annihilation WHERE End_time > "${dayjs().format(
        "YYYY-MM-DD HH:mm:ss"
      )}" AND Agree=1`;
      db.query(sql3, (err, result) => {
        if (err) {
          return;
        }
        // 活动转化
        result.some((item) => {
          item.Login_rewards = JSON.parse(item.Login_rewards);
          item.Bonus_character = JSON.parse(item.Bonus_character);
          item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
          item.Checkin_rewards = JSON.parse(item.Checkin_rewards);
          item.Signs_of_calamity = JSON.parse(item.Signs_of_calamity);
          item.Scattered_configuration = JSON.parse(
            item.Scattered_configuration
          );
          item.Strategy = JSON.parse(item.Strategy);
          item.Marketplace_Redemption1 = JSON.parse(
            item.Marketplace_Redemption1
          );
          item.Marketplace_Redemption2 = JSON.parse(
            item.Marketplace_Redemption2
          );
          item.Marketplace_Redemption3 = JSON.parse(
            item.Marketplace_Redemption3
          );
          item.Mission_rewards = JSON.parse(item.Mission_rewards);
          item.No_day_mission = JSON.parse(item.No_day_mission);
          item["is"] = "封灭战";
          // 活动开始时间——————————————————————————————————————————————————————————————
          if (item.Start_time !== null) {
            let Begin = new Date(item.Start_time);
            // 赋值开始时间
            item.Start_time = dayjs(item.Start_time + 86400000).format(
              "YYYY-MM-DD HH:mm:ss"
            );
          }
          // 活动结束时间——————————————————————————————————————————————————————
          if (item.End_time !== null) {
            // 赋值结束时间
            item.End_time = dayjs(item.End_time + 86400000).format(
              "YYYY-MM-DD HH:mm:ss"
            );
          }
          // 赋值输出数组
          output.push(item);
        });

        res.json({
          data: output,
          success: true,
        });
      });
    });
  });
});
// 查看正式库活动接口———————————————————————————————————————————————————————————————————
router.get("/TotalActivity", (req, res) => {
  console.log("查看正式库活动接口");
  // 输出数据
  let output = [];
  // 查数据库
  let sql = `SELECT * from activity WHERE Agree=1 `;
  req.query.name
    ? (sql = `SELECT * from activity WHERE Agree=1 AND name LIKE '%${req.query.name}%' `)
    : "";
  db.query(sql, (err, result) => {
    if (err) {
      return;
    }
    // 活动转化
    result.some((item) => {
      item.Login_rewards = JSON.parse(item.Login_rewards);
      item.Bonus_character = JSON.parse(item.Bonus_character);
      item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
      item.Checkin_rewards = JSON.parse(item.Checkin_rewards);
      item.Marketplace_Redemption = JSON.parse(item.Marketplace_Redemption);
      item.Mission_rewards = JSON.parse(item.Mission_rewards);
      item.No_day_mission = JSON.parse(item.No_day_mission);
      item["is"] = "活动";
      // 活动开始时间——————————————————————————————————————————————————————————————
      if (item.Start_time !== null) {
        let Begin = new Date(item.Start_time);
        // 赋值开始时间
        item.Start_time = dayjs(item.Start_time + 86400000).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      // 活动结束时间——————————————————————————————————————————————————————
      if (item.End_time !== null) {
        // 赋值结束时间
        item.End_time = dayjs(item.End_time + 86400000).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      // 赋值输出数据
      output.push(item);
    });
    // 查调伏战
    let sql2 = `SELECT * from ambush_warfare WHERE Agree=1`;
    req.query.name
      ? (sql2 = `SELECT * from ambush_warfare WHERE Agree=1 AND name LIKE '%${req.query.name}%' `)
      : "";
    db.query(sql2, (err, result) => {
      if (err) {
        return;
      }
      // 活动转化
      result.some((item) => {
        item.Login_rewards = JSON.parse(item.Login_rewards);
        item.Bonus_character = JSON.parse(item.Bonus_character);
        item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
        item.Checkin_rewards = JSON.parse(item.Checkin_rewards);
        item.Signs_of_calamity = JSON.parse(item.Signs_of_calamity);
        item.Scattered_configuration = JSON.parse(item.Scattered_configuration);
        item.Strategy = JSON.parse(item.Strategy);
        item.Marketplace_Redemption1 = JSON.parse(item.Marketplace_Redemption1);
        item.Marketplace_Redemption2 = JSON.parse(item.Marketplace_Redemption2);
        item.Mission_rewards = JSON.parse(item.Mission_rewards);
        item.No_day_mission = JSON.parse(item.No_day_mission);
        item["is"] = "调伏战";
        // 活动开始时间——————————————————————————————————————————————————————————————
        if (item.Start_time !== null) {
          let Begin = new Date(item.Start_time);
          // 赋值开始时间
          item.Start_time = dayjs(item.Start_time + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
        // 活动结束时间——————————————————————————————————————————————————————
        if (item.End_time !== null) {
          // 赋值结束时间
          item.End_time = dayjs(item.End_time + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
        // 赋值输出数据
        output.push(item);
      });
      // 查封灭战
      let sql2 = `SELECT * from war_of_annihilation WHERE Agree=1`;
      req.query.name
        ? (sql2 = `SELECT * from war_of_annihilation WHERE Agree=1 AND name LIKE '%${req.query.name}%' `)
        : "";
      db.query(sql2, (err, result) => {
        if (err) {
          return;
        }
        // 活动转化
        result.some((item) => {
          item.Login_rewards = JSON.parse(item.Login_rewards);
          item.Bonus_character = JSON.parse(item.Bonus_character);
          item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
          item.Checkin_rewards = JSON.parse(item.Checkin_rewards);
          item.Signs_of_calamity = JSON.parse(item.Signs_of_calamity);
          item.Scattered_configuration = JSON.parse(
            item.Scattered_configuration
          );
          item.Strategy = JSON.parse(item.Strategy);
          item.Marketplace_Redemption1 = JSON.parse(
            item.Marketplace_Redemption1
          );
          item.Marketplace_Redemption2 = JSON.parse(
            item.Marketplace_Redemption2
          );
          item.Marketplace_Redemption3 = JSON.parse(
            item.Marketplace_Redemption3
          );
          item.Mission_rewards = JSON.parse(item.Mission_rewards);
          item.No_day_mission = JSON.parse(item.No_day_mission);
          item["is"] = "封灭战";
          // 活动开始时间——————————————————————————————————————————————————————————————
          if (item.Start_time !== null) {
            let Begin = new Date(item.Start_time);
            // 赋值开始时间
            item.Start_time = dayjs(item.Start_time + 86400000).format(
              "YYYY-MM-DD HH:mm:ss"
            );
          }
          // 活动结束时间——————————————————————————————————————————————————————
          if (item.End_time !== null) {
            // 赋值结束时间
            item.End_time = dayjs(item.End_time + 86400000).format(
              "YYYY-MM-DD HH:mm:ss"
            );
          }
          // 赋值输出数据
          output.push(item);
        });
        res.json({
          data: output,
          success: true,
        });
      });
    });
  });
});
// 查看正式库活动详细接口————————————————————————————————————————————————————————————————————
router.get("/TheEventIsDetailed", (req, res) => {
  console.log("查看正式库活动详细接口");
  // 判断id参数是否有传参
  if (req.query.id === undefined || req.query.id === "") {
    res.send({
      msg: "未知错误",
      success: false,
    });
  } else {
    // 查数据库
    const sql = `SELECT * from activity WHERE id="${req.query.id}"`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      // 转化部分数组成js格式与时间矫正
      result.some((item) => {
        item.Login_rewards = JSON.parse(item.Login_rewards);
        item.Bonus_character = JSON.parse(item.Bonus_character);
        item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
        item.Checkin_rewards = JSON.parse(item.Checkin_rewards);
        item.Marketplace_Redemption = JSON.parse(item.Marketplace_Redemption);
        item.Mission_rewards = JSON.parse(item.Mission_rewards);
        item.No_day_mission = JSON.parse(item.No_day_mission);
        // 活动开始时间——————————————————————————————————————————————————————————————
        if (item.Start_time !== null) {
          let Begin = new Date(item.Start_time);
          // 赋值开始时间
          item.Start_time = dayjs(item.Start_time + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
        // 活动结束时间——————————————————————————————————————————————————————
        if (item.End_time !== null) {
          // 赋值结束时间
          item.End_time = dayjs(item.End_time + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
      });

      if (result.length === 0) {
        res.send({
          msg: "未查找到对应数据",
          success: false,
        });
      } else {
        res.json({
          data: result[0],
          success: true,
        });
      }
    });
  }
});
// 查看正式库调伏战详细接口————————————————————————————————————————————————————————————————————
router.get("/AmbushWarfare", (req, res) => {
  console.log("查看正式库调伏战详细接口");
  // 判断id参数是否有传参
  if (req.query.id === undefined || req.query.id === "") {
    res.send({
      msg: "未知错误",
      success: false,
    });
  } else {
    // 查数据库
    const sql = `SELECT * from ambush_warfare WHERE id="${req.query.id}"`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      // 转化部分数组成js格式与时间矫正
      result.some((item) => {
        item.Login_rewards = JSON.parse(item.Login_rewards);
        item.Bonus_character = JSON.parse(item.Bonus_character);
        item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
        item.Checkin_rewards = JSON.parse(item.Checkin_rewards);
        item.Signs_of_calamity = JSON.parse(item.Signs_of_calamity);
        item.Scattered_configuration = JSON.parse(item.Scattered_configuration);
        item.Strategy = JSON.parse(item.Strategy);
        item.Marketplace_Redemption1 = JSON.parse(item.Marketplace_Redemption1);
        item.Marketplace_Redemption2 = JSON.parse(item.Marketplace_Redemption2);
        item.Mission_rewards = JSON.parse(item.Mission_rewards);
        item.No_day_mission = JSON.parse(item.No_day_mission);
        item.Created_Data !== null
          ? dayjs(item.Created_Data + 86400000).format("YYYY-MM-DD")
          : "";
        // 活动开始时间——————————————————————————————————————————————————————————————
        if (item.Start_time !== null) {
          // 赋值开始时间
          item.Start_time = dayjs(item.Start_time + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
        // 活动结束时间——————————————————————————————————————————————————————
        if (item.End_time !== null) {
          // 赋值结束时间
          item.End_time = dayjs(item.End_time + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
      });

      if (result.length === 0) {
        res.send({
          msg: "未查找到对应数据",
          success: false,
        });
      } else {
        res.json({
          data: result[0],
          success: true,
        });
      }
    });
  }
});
// 查看正式库正在进行卡池接口———————————————————————————————————————————————————————————————————
router.get("/HoldACardPool", (req, res) => {
  console.log("查看正式库正在进行卡池接口");
  // 查数据库
  const sql = `SELECT * from card_pools WHERE End_time > "${dayjs().format(
    "YYYY-MM-DD HH:mm:ss"
  )}" AND Agree=1`;
  db.query(sql, (err, result) => {
    if (err) {
      return;
    }
    // 活动转化
    result.some((item) => {
      item.Bonus_character = JSON.parse(item.Bonus_character);
      item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
      // 活动开始时间——————————————————————————————————————————————————————————————
      if (item.Start_time !== null) {
        let Begin = new Date(item.Start_time);
        // 赋值开始时间
        item.Start_time = dayjs(item.Start_time + 86400000).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      // 活动结束时间——————————————————————————————————————————————————————
      if (item.End_time !== null) {
        // 赋值结束时间
        item.End_time = dayjs(item.End_time + 86400000).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
    });
    res.json({
      data: result,
      success: true,
    });
  });
});
// 查看正式库卡池接口———————————————————————————————————————————————————————————————————
router.get("/ACardPool", (req, res) => {
  console.log("查看正式库活动接口");
  // 查数据库
  let sql = `SELECT * from card_pools WHERE Agree=1`;
  req.query.name
    ? (sql = `SELECT * from card_pools WHERE Agree=1 AND name LIKE '%${req.query.name}%' `)
    : "";
  db.query(sql, (err, result) => {
    if (err) {
      return;
    }
    // 活动转化
    result.some((item) => {
      item.Bonus_character = JSON.parse(item.Bonus_character);
      item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
      // 活动开始时间——————————————————————————————————————————————————————————————
      if (item.Start_time !== null) {
        let Begin = new Date(item.Start_time);
        // 赋值开始时间
        item.Start_time = dayjs(item.Start_time + 86400000).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      // 活动结束时间——————————————————————————————————————————————————————
      if (item.End_time !== null) {
        // 赋值结束时间
        item.End_time = dayjs(item.End_time + 86400000).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
    });

    res.json({
      data: result,
      success: true,
    });
  });
});
// 查看正式库卡池详细接口————————————————————————————————————————————————————————————————————
router.get("/DetailCardPool", (req, res) => {
  console.log("查看正式库活动详细接口");
  // 判断id参数是否有传参
  if (req.query.id === undefined || req.query.id === "") {
    res.send({
      msg: "未知错误",
      success: false,
    });
  } else {
    // 查数据库
    const sql = `SELECT * from card_pools WHERE id="${req.query.id}"`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      // 转化部分数组成js格式与时间矫正
      result.some((item) => {
        item.Bonus_character = JSON.parse(item.Bonus_character);
        item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
        // 活动开始时间——————————————————————————————————————————————————————————————
        if (item.Start_time !== null) {
          let Begin = new Date(item.Start_time);
          // 赋值开始时间
          item.Start_time = dayjs(item.Start_time + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
        // 活动结束时间——————————————————————————————————————————————————————
        if (item.End_time !== null) {
          // 赋值结束时间
          item.End_time = dayjs(item.End_time + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
      });

      if (result.length === 0) {
        res.send({
          msg: "未查找到对应数据",
          success: false,
        });
      } else {
        res.json({
          data: result[0],
          success: true,
        });
      }
    });
  }
});
// 查看排行榜接口——————————————————————————————————————————————————————————————————
router.get("/list", (req, res) => {
  console.log("查看wiki排行榜接口");
  // 查询角色表
  let sql = "";
  if (
    req.query.state === "0" ||
    req.query.state === "1" ||
    req.query.state === "2" ||
    req.query.state === "3"
  ) {
    sql = `SELECT * from leaderboard WHERE id="${req.query.state}"`;
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    if (result.length === 0) {
      res.send({
        msg: "查询失败",
        success: false,
      });
    } else {
      // 调伏战转化
      result.some((item) => {
        item.content = JSON.parse(item.content);
        item.AgreeData !== null
          ? dayjs(item.AgreeData + 86400000).format("YYYY-MM-DD HH:mm:ss")
          : "";
      });
      return res.send({
        data: result[0],
        success: true,
      });
    }
  });
});
// 查看角色灯幻镜枚举接口——————————————————————————————————————————————————————————————————
router.get("/enumerate", (req, res) => {
  console.log("查看枚举");
  // 查询角色表
  let sql = "";
  // 是否层级  true是，false不是
  let state = true;
  let row = [];
  if (req.query.classify && !req.query.levelName) {
    // 层级
    sql = `SELECT * from enum WHERE classify="${req.query.classify}" AND level='1'`;
    state = true;
  } else if (req.query.classify && req.query.levelName) {
    // 详细
    sql = `SELECT * from enum WHERE classify="${req.query.classify}" AND level='2' AND levelName='${req.query.levelName}'`;
    state = false;
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }

  // 查找数据库第一层
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    if (result.length === 0) {
      res.send({
        msg: "查询失败",
        success: false,
      });
    } else {
      // 转化
      result.some((item, index) => {
        // 转化时间
        item.Created_Data !== null
          ? (item.Created_Data = dayjs(item.Created_Data + 86400000).format(
              "YYYY-MM-DD"
            ))
          : "";
        item["content"] = [];
        // 一层赋值给输出字段
        row.push(item);
      });
      // 判断如果是找详细的话就直接输出
      if (state === true) {
        let sql2 = `SELECT * from enum WHERE classify="${req.query.classify}" AND level='2'`;
        // 查二层
        db.query(sql2, function (err, result2) {
          // 把对应二层赋值给对应一层内容字段中去
          // row[index]["content"] = result2;
          // 循环一层
          row.some((item) => {
            // 循环二层数据并重新赋值日期
            result2.some((item3) => {
              item3.Created_Data !== null
                ? (item3.Created_Data = dayjs(
                    item3.Created_Data + 86400000
                  ).format("YYYY-MM-DD"))
                : "";
              item.levelName === item3.levelName
                ? item.content.push(item3)
                : "";
            });
          });
          res.json({
            data: row,
            success: true,
          });
        });
      } else if (state === false) {
        res.json({
          data: row,
          success: true,
        });
      }
    }
  });
});
// 查看正式库正在进行活动、调伏战、卡池接口———————————————————————————————————————————————————————————————————
router.get("/Ongoing", (req, res) => {
  console.log("查看正式库正在进行活动、调伏战、卡池接口");
  // 输出数据
  let output = [];
  // 查数据库
  const sql = `SELECT * from activity WHERE End_time > "${dayjs().format(
    "YYYY-MM-DD HH:mm:ss"
  )}" AND Agree=1`;
  db.query(sql, (err, result) => {
    if (err) {
      return;
    }
    // 活动转化
    result.some((item) => {
      item.Login_rewards = JSON.parse(item.Login_rewards);
      item.Bonus_character = JSON.parse(item.Bonus_character);
      item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
      item.Checkin_rewards = JSON.parse(item.Checkin_rewards);
      item.Marketplace_Redemption = JSON.parse(item.Marketplace_Redemption);
      item.Mission_rewards = JSON.parse(item.Mission_rewards);
      item.No_day_mission = JSON.parse(item.No_day_mission);
      item["is"] = "活动";
      item["url"] = item.Activity_diagram_url;
      // 活动开始时间——————————————————————————————————————————————————————————————
      if (item.Start_time !== null) {
        let Begin = new Date(item.Start_time);
        // 赋值开始时间
        item.Start_time = dayjs(item.Start_time + 86400000).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      // 活动结束时间——————————————————————————————————————————————————————
      if (item.End_time !== null) {
        // 赋值结束时间
        item.End_time = dayjs(item.End_time + 86400000).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      // 赋值输出数组
      output.push(item);
    });

    // 查数据库
    const sql2 = `SELECT * from ambush_warfare WHERE End_time > "${dayjs().format(
      "YYYY-MM-DD HH:mm:ss"
    )}" AND Agree=1`;
    db.query(sql2, (err, result) => {
      if (err) {
        return;
      }
      // 活动转化
      result.some((item) => {
        item.Login_rewards = JSON.parse(item.Login_rewards);
        item.Bonus_character = JSON.parse(item.Bonus_character);
        item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
        item.Checkin_rewards = JSON.parse(item.Checkin_rewards);
        item.Signs_of_calamity = JSON.parse(item.Signs_of_calamity);
        item.Scattered_configuration = JSON.parse(item.Scattered_configuration);
        item.Strategy = JSON.parse(item.Strategy);
        item.Marketplace_Redemption1 = JSON.parse(item.Marketplace_Redemption1);
        item.Marketplace_Redemption2 = JSON.parse(item.Marketplace_Redemption2);
        item.Mission_rewards = JSON.parse(item.Mission_rewards);
        item.No_day_mission = JSON.parse(item.No_day_mission);
        item["is"] = "调伏战";
        item["url"] = item.Activity_diagram_url;
        // 活动开始时间——————————————————————————————————————————————————————————————
        if (item.Start_time !== null) {
          let Begin = new Date(item.Start_time);
          // 赋值开始时间
          item.Start_time = dayjs(item.Start_time + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
        // 活动结束时间——————————————————————————————————————————————————————
        if (item.End_time !== null) {
          // 赋值结束时间
          item.End_time = dayjs(item.End_time + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
        // 赋值输出数组
        output.push(item);
      });

      // 正在进行卡池
      const sql = `SELECT * from card_pools WHERE End_time > "${dayjs().format(
        "YYYY-MM-DD HH:mm:ss"
      )}" AND Agree=1`;
      db.query(sql, (err, result) => {
        if (err) {
          return;
        }
        // 活动转化
        result.some((item) => {
          item.Bonus_character = JSON.parse(item.Bonus_character);
          item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
          item["is"] = "卡池";
          item["url"] = item.Card_pools_url;
          // 活动开始时间——————————————————————————————————————————————————————————————
          if (item.Start_time !== null) {
            let Begin = new Date(item.Start_time);
            // 赋值开始时间
            item.Start_time = dayjs(item.Start_time + 86400000).format(
              "YYYY-MM-DD HH:mm:ss"
            );
          }
          // 活动结束时间——————————————————————————————————————————————————————
          if (item.End_time !== null) {
            // 赋值结束时间
            item.End_time = dayjs(item.End_time + 86400000).format(
              "YYYY-MM-DD HH:mm:ss"
            );
          }
          // 赋值输出数组
          output.push(item);
        });
        // 查数据库
        const sql3 = `SELECT * from war_of_annihilation WHERE End_time > "${dayjs().format(
          "YYYY-MM-DD HH:mm:ss"
        )}" AND Agree=1`;
        db.query(sql3, (err, result) => {
          if (err) {
            return;
          }
          // 活动转化
          result.some((item) => {
            item.Login_rewards = JSON.parse(item.Login_rewards);
            item.Bonus_character = JSON.parse(item.Bonus_character);
            item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
            item.Checkin_rewards = JSON.parse(item.Checkin_rewards);
            item.Signs_of_calamity = JSON.parse(item.Signs_of_calamity);
            item.Scattered_configuration = JSON.parse(
              item.Scattered_configuration
            );
            item.Strategy = JSON.parse(item.Strategy);
            item.Marketplace_Redemption1 = JSON.parse(
              item.Marketplace_Redemption1
            );
            item.Marketplace_Redemption2 = JSON.parse(
              item.Marketplace_Redemption2
            );
            item.Marketplace_Redemption3 = JSON.parse(
              item.Marketplace_Redemption3
            );
            item.Mission_rewards = JSON.parse(item.Mission_rewards);
            item.No_day_mission = JSON.parse(item.No_day_mission);
            item["is"] = "封灭战";
            item["url"] = item.Activity_diagram_url;
            // 活动开始时间——————————————————————————————————————————————————————————————
            if (item.Start_time !== null) {
              let Begin = new Date(item.Start_time);
              // 赋值开始时间
              item.Start_time = dayjs(item.Start_time + 86400000).format(
                "YYYY-MM-DD HH:mm:ss"
              );
            }
            // 活动结束时间——————————————————————————————————————————————————————
            if (item.End_time !== null) {
              // 赋值结束时间
              item.End_time = dayjs(item.End_time + 86400000).format(
                "YYYY-MM-DD HH:mm:ss"
              );
            }
            // 赋值输出数组
            output.push(item);
          });
          res.json({
            data: output,
            success: true,
          });
        });
      });
    });
  });
});
// 查看正式库新增角色、灯幻镜、活动接口———————————————————————————————————————————————————————————————————
router.get("/New", (req, res) => {
  console.log("查看正式库新增角色、灯幻镜、活动接口");
  // 获取当月的的1号和当月最后一天
  let startOf = dayjs(new Date()).startOf("month").format("YYYY-MM-DD");
  let endOf = dayjs(new Date()).endOf("month").format("YYYY-MM-DD");
  // 输出数据
  let output = {
    // 角色
    role: [],
    // 灯幻镜
    LampMirror: [],
    // 活动
    activity: [],
  };
  // 查新增角色
  const sql = `SELECT id,name,Standing_painting_fales FROM character_guide WHERE Created_Data  BETWEEN "${startOf}" AND "${endOf}"  AND Agree=1 ORDER BY id desc `;
  db.query(sql, (err, result) => {
    if (err) {
      return;
    }
    // 活动转化
    result.some((item) => {
      if (output.role.length < 12) {
        // 赋值输出数组
        output.role.push(item);
      }
    });

    // 查灯幻镜库
    const sql2 = `SELECT id,name,Lamp_Mirror_url FROM slide_projector WHERE Created_Data  BETWEEN "${startOf}" AND "${endOf}" AND Agree=1 ORDER BY id desc `;
    db.query(sql2, (err, result) => {
      if (err) {
        return;
      }
      // 活动转化
      result.some((item) => {
        if (output.LampMirror.length < 12) {
          // 赋值输出数组
          output.LampMirror.push(item);
        }
      });

      // 查活动
      const sql = `SELECT id,name,Activity_icon_url FROM ambush_warfare WHERE Created_Data  BETWEEN "${startOf}" AND "${endOf}" AND Agree=1 ORDER BY id desc `;
      db.query(sql, (err, result) => {
        if (err) {
          return;
        }
        // 活动转化
        result.some((item) => {
          item["is"] = "调伏战";

          if (output.activity.length < 12) {
            // 赋值输出数组
            output.activity.push(item);
          }
        });
        // 查调伏战
        const sql = `SELECT id,name,Activity_icon_url FROM activity WHERE Created_Data  BETWEEN "${startOf}" AND "${endOf}" AND Agree=1 ORDER BY id desc`;
        db.query(sql, (err, result) => {
          if (err) {
            return;
          }
          // 活动转化
          result.some((item) => {
            item["is"] = "活动";

            if (output.activity.length < 12) {
              // 赋值输出数组
              output.activity.push(item);
            }
          });
          // 查调伏战封灭战
          const sql = `SELECT id,name,Activity_icon_url FROM war_of_annihilation WHERE Created_Data  BETWEEN "${startOf}" AND "${endOf}" AND Agree=1 ORDER BY id desc`;
          db.query(sql, (err, result) => {
            if (err) {
              return;
            }
            // 活动转化
            result.some((item) => {
              item["is"] = "封灭战";

              if (output.activity.length < 12) {
                // 赋值输出数组
                output.activity.push(item);
              }
            });
            res.json({
              data: output,
              success: true,
            });
          });
        });
      });
    });
  });
});
// wiki查看抽卡模拟器卡池接口——————————————————————————————————————————————————————————————————
router.get("/EmulateACardPool", (req, res) => {
  console.log("wiki查看抽卡模拟器卡池接口");
  // 查询角色表
  let sql = "";
  // 输出数据
  let data = [];
  if (req.query.state === "进行中") {
    sql = `SELECT * from simulated_gacha WHERE End_time > "${dayjs().format(
      "YYYY-MM-DD HH:mm:ss"
    )}" ORDER BY id desc`;
  } else if (req.query.state === "已结束") {
    sql = `SELECT * from simulated_gacha WHERE End_time < "${dayjs().format(
      "YYYY-MM-DD HH:mm:ss"
    )}" ORDER BY id desc`;
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }

  // 查找数据库
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    // 调伏战转化
    result.some((item) => {
      item.Bonus_character = JSON.parse(item.Bonus_character);
      item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
      item.Start_time !== null
        ? (item.Start_time = dayjs(item.Start_time + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          ))
        : "";
      item.End_time !== null
        ? (item.End_time = dayjs(item.End_time + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          ))
        : "";

      if (req.query.state === "进行中") {
        data.push(item);
      } else if (req.query.state === "已结束") {
        data.length < 3 ? data.push(item) : "";
      }
    });
    res.json({
      data: data,
      success: true,
    });
  });
});
// wiki判断宝石是否重置接口——————————————————————————————————————————————————————————————————
router.get("/ResetTheGems", (req, res) => {
  console.log("wiki判断宝石是否重置接口");
  // 查找数据库
  const sql = `SELECT * from leaderboard WHERE id="3"`;
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    if (result.length === 0) {
      res.send({
        msg: "查询失败",
        success: false,
      });
    } else {
      // 数据库数据
      let item = result[0];
      let sql2 = "";
      if (dayjs().format("d") !== "1" && item.state === "1") {
        // 判断是否不是周一且状态为不可修改
        sql2 = `UPDATE leaderboard SET state='0'  WHERE id=3`;
      } else if (dayjs().format("d") === "1" && item.state === "0") {
        //  判断是否是周一且状态为要修改，则重置宝石数和修改修改状态
        sql2 = `UPDATE leaderboard SET state='1',num=60000  WHERE id=3`;
      } else {
        return res.json({
          msg: "还未到重置时间",
          success: false,
        });
      }
      // 进行数据库修改操作
      db.query(sql2, function (err, rows) {
        if (err) {
          res.send({
            msg: "重置失败",
            success: false,
          });
        } else {
          res.send({
            msg: "重置成功",
            success: true,
          });
        }
      });
    }
  });
});
// wiki查询详细抽卡模拟器卡池接口——————————————————————————————————————————————————————————————————
router.get("/ResetTheGemsDetails", (req, res) => {
  console.log("wiki查询详细抽卡模拟器卡池接口");
  if (req.query.id) {
    let date = {
      // 卡池信息
      Information: {},
      // 角色
      role: [],
      // 灯幻镜
      LampMirror: [],
    };
    // 查模拟卡池详细信息
    const sql = `SELECT * from simulated_gacha WHERE id="${req.query.id}"`;
    db.query(sql, function (err, result) {
      if (err) {
        return;
      }
      if (result.length === 0) {
        res.send({
          msg: "查询失败",
          success: false,
        });
      } else {
        // 抽卡模拟器装换转化
        result.some((item) => {
          item.Bonus_character = JSON.parse(item.Bonus_character);
          item.Bonus_character.some((item) => {
            item["is"] = "角色";
            item["UP"] = true;
          });
          item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
          item.Bonus_mirror.some((item) => {
            item["is"] = "灯幻镜";
            item["UP"] = true;
          });
          item.Start_time !== null
            ? (item.Start_time = dayjs(item.Start_time + 86400000).format(
                "YYYY-MM-DD HH:mm:ss"
              ))
            : "";
          item.End_time !== null
            ? (item.End_time = dayjs(item.End_time + 86400000).format(
                "YYYY-MM-DD HH:mm:ss"
              ))
            : "";
        });
        // 卡池详细信息赋值
        date.Information = result[0];
        // 查询常驻卡池角色
        const sql2 = `SELECT * from character_guide WHERE obtain="常驻卡池"`;
        db.query(sql2, function (err, result) {
          if (err) {
            return;
          }
          // 抽卡模拟器装换转化
          result.some((item) => {
            item["is"] = "角色";
            item["url"] = item.Standing_painting_fales;
            item["UP"] = false;
            date.role.push(item);
          });
          // 查询常驻灯幻镜
          const sql3 = `SELECT * from slide_projector WHERE obtain="常驻卡池" AND Agree=1`;
          db.query(sql3, function (err, result) {
            if (err) {
              return;
            }
            // 抽卡模拟器装换转化
            result.some((item) => {
              item["is"] = "灯幻镜";
              item["url"] = item.Lamp_Mirror_url;
              item["UP"] = false;
              date.LampMirror.push(item);
            });
            // 输出结果
            res.send({
              data: date,
              success: true,
            });
          });
        });
      }
    });
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
});
// wiki抽卡模拟器扣除宝石接口——————————————————————————————————————————————————————————————————
router.post("/GemstoneDeductions", (req, res) => {
  console.log("wiki抽卡模拟器扣除宝石");
  // 查找数据库
  const sql = `SELECT * from leaderboard WHERE id="3"`;
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    if (result.length === 0) {
      res.send({
        msg: "查询失败",
        success: false,
      });
    } else {
      // 数据库数据
      let item = result[0];
      let sql2 = "";
      if (req.body.state === 0) {
        // 单抽
        // 判断宝石数量够不够
        if (item.num - 300 < 0) {
          return res.json({
            msg: "宝石数量不足，请联系管理员进行充值",
            success: false,
          });
        } else {
          sql2 = `UPDATE leaderboard SET num=${item.num - 300}  WHERE id=3`;
        }
      } else if (req.body.state === 1) {
        // 十连

        // 判断宝石数量够不够
        if (item.num - 3000 < 0) {
          return res.json({
            msg: "宝石数量不足，请联系管理员进行充值",
            success: false,
          });
        } else {
          sql2 = `UPDATE leaderboard SET num=${item.num - 3000}  WHERE id=3`;
        }
      } else {
        return res.json({
          msg: "出现未知错误",
          success: false,
        });
      }
      // 进行数据库修改操作
      db.query(sql2, function (err, rows) {
        if (err) {
          res.send({
            msg: "抽取失败",
            success: false,
          });
        } else {
          res.send({
            msg: "抽取成功",
            success: true,
          });
        }
      });
    }
  });
});
// wiki抽卡模拟器判断抽卡结果增加宝石接口——————————————————————————————————————————————————————————————————
router.post("/GemsIncrease", (req, res) => {
  console.log("wiki抽卡模拟器判断抽卡结果增加宝石");
  // 查找数据库
  const sql = `SELECT * from leaderboard WHERE id="3"`;
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }
    if (result.length === 0) {
      res.send({
        msg: "查询失败",
        success: false,
      });
    } else {
      // 数据库数据
      let item = result[0];
      let sql2 = "";
      let num = 0;
      if (req.body.res) {
        req.body.res.some((item2) => {
          if (
            item2.UP === false &&
            item2.is === "角色" &&
            item2.initial === 3
          ) {
            // 三星角色
            num = num + parseInt(item.role3);
          } else if (
            item2.UP === false &&
            item2.is === "灯幻镜" &&
            item2.initial === 3
          ) {
            // 三星灯幻镜
            num = num + parseInt(item.LampMirror3);
          } else if (item2.UP === true && item2.is === "角色") {
            // 三星UP角色
            num = num + parseInt(item.role3UP);
          } else if (item2.UP === true && item2.is === "灯幻镜") {
            // 三星UP灯幻镜
            num = num + parseInt(item.LampMirror3UP);
          }
        });
        console.log("获得");
        console.log(num);
        sql2 = `UPDATE leaderboard SET num=${item.num + num}  WHERE id=3`;
      } else {
        return res.json({
          msg: "出现未知错误",
          success: false,
        });
      }
      // 进行数据库修改操作
      db.query(sql2, function (err, rows) {
        if (err) {
          res.send({
            msg: "卡池抽取失败",
            success: false,
          });
        } else {
          res.send({
            msg: `合计获得宝石${num}`,
            success: true,
          });
        }
      });
    }
  });
});
//wiki获取遗迹首页数据接口——————————————————————————————————————————————————————————————————
router.get("/RelicsEditData", (req, res) => {
  console.log("/wiki获取遗迹首页数据");
  // 查询角色表
  let sql = "";
  let row = [];
  if (!req.query.name) {
    sql = `SELECT * from enum WHERE classify='遗迹' ORDER BY id desc`;
  } else {
    sql = `SELECT * from enum WHERE classify='遗迹' AND name LIKE '%${req.query.name}%' ORDER BY id desc`;
  }
  // 层级

  // 查找数据库第一层
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }

    // 转化
    result.some((item, index) => {
      // 转化时间
      item.Created_Data !== null
        ? (item.Created_Data = dayjs(item.Created_Data + 86400000).format(
            "YYYY-MM-DD"
          ))
        : "";
      item["content"] = [];
      // 一层赋值给输出字段
      row.push(item);
    });
    let sql2 = `SELECT * from relic`;
    // 查二层
    db.query(sql2, function (err, result2) {
      // 把对应二层赋值给对应一层内容字段中去
      // 循环一层
      row.some((item) => {
        // 循环二层数据并重新赋值日期
        result2.some((item3) => {
          item3.Created_Data !== null
            ? (item3.Created_Data = dayjs(item3.Created_Data + 86400000).format(
                "YYYY-MM-DD"
              ))
            : "";
          parseInt(item.id) === item3.nameID ? item.content.push(item3) : "";
        });
      });
      res.json({
        data: row,
        success: true,
      });
    });
  });
});
// 获取遗迹导入层数数据接口——————————————————————————————————————————————————————————————————
router.get("/ImportTheRuins", (req, res) => {
  console.log("wiki获取遗迹导入层数数据接口");
  // 查询表
  if (req.query.id) {
    let sql = `SELECT * from relic WHERE nameID=${req.query.id}`;
    console.log(sql);
    // 查找数据库
    db.query(sql, function (err, result) {
      if (err) {
        return;
      }
      result.some((item) => {
        item.after1 = JSON.parse(item.after1);
        item.after2 = JSON.parse(item.after2);
        item.after3 = JSON.parse(item.after3);
        item.former1 = JSON.parse(item.former1);
        item.former2 = JSON.parse(item.former2);
        item.former3 = JSON.parse(item.former3);
        !item.Created_Data
          ? ""
          : (item.Created_Data = dayjs(item.Created_Data + 86400000).format(
              "YYYY-MM-DD"
            ));
      });
      res.send({
        data: result,
        success: true,
      });
    });
  } else {
    res.send({
      msg: "获取遗迹导入层数数据失败",
      success: false,
    });
  }
});
//获取新闻公告接口——————————————————————————————————————————————————————————————————
router.get("/PressReleases", (req, res) => {
  console.log("获取新闻公告");
  // 查询角色表
  let row = {
    // 置顶
    sticky: [],
    // 非置顶
    stickyNo: [],
  };
  let Total = 1;
  // 判断有没有传分页和每条数量
  if (req.query.page && req.query.size) {
    let sql =
      "SELECT * from press_releases WHERE sticky='是' ORDER BY noticeDate desc";
    // 查找数据库置顶
    db.query(sql, function (err, result) {
      if (err) {
        return;
      }

      // 转化
      result.some((item, index) => {
        // 转化时间
        item.Created_Data !== null
          ? (item.Created_Data = dayjs(item.Created_Data + 86400000).format(
              "YYYY-MM-DD"
            ))
          : "";
        // 转化时间
        item.noticeDate !== null
          ? (item.noticeDate = dayjs(item.noticeDate + 86400000).format(
              "YYYY-MM-DD HH:mm:ss"
            ))
          : "";
      });
      row.sticky = result;
      let sql2 = "";
      if (!req.query.name) {
        sql2 = `SELECT * from press_releases WHERE sticky='否' ORDER BY noticeDate desc`;
      } else {
        sql2 = `SELECT * from press_releases WHERE sticky='否' AND name LIKE '%${req.query.name}%' ORDER BY noticeDate desc`;
      }
      // 查非置顶消息
      db.query(sql2, function (err, result2) {
        if (err) {
          return;
        }

        // 转化
        result2.some((item, index) => {
          // 转化时间
          item.Created_Data !== null
            ? (item.Created_Data = dayjs(item.Created_Data + 86400000).format(
                "YYYY-MM-DD"
              ))
            : "";
          // 转化时间
          item.noticeDate !== null
            ? (item.noticeDate = dayjs(item.noticeDate + 86400000).format(
                "YYYY-MM-DD HH:mm:ss"
              ))
            : "";
        });
        // 赋值合计页数
        Total = result2.length;
        // 赋值非置顶数据
        row.stickyNo = result2.splice(
          parseInt(req.query.page) * parseInt(req.query.size) -
            parseInt(req.query.size),
          parseInt(req.query.size)
        );
        res.json({
          data: row,
          Total: Total,
          success: true,
        });
      });
    });
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
});
// 查看公告详细接口————————————————————————————————————————————————————————————————————
router.get("/AnnouncementsOfDetails", (req, res) => {
  console.log("查看公告详细");
  // 判断id参数是否有传参
  if (!req.query.id) {
    res.send({
      msg: "未知错误",
      success: false,
    });
  } else {
    // 查数据库
    const sql = `SELECT * from press_releases WHERE id="${req.query.id}"`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      // 转化部分数组成js格式与时间矫正
      result.some((item) => {
        // 转化时间
        item.Created_Data !== null
          ? (item.Created_Data = dayjs(item.Created_Data + 86400000).format(
              "YYYY-MM-DD"
            ))
          : "";
        // 转化时间
        item.noticeDate !== null
          ? (item.noticeDate = dayjs(item.noticeDate + 86400000).format(
              "YYYY-MM-DD HH:mm:ss"
            ))
          : "";
      });

      if (result.length === 0) {
        res.send({
          msg: "未查找到对应数据",
          success: false,
        });
      } else {
        res.json({
          data: result[0],
          success: true,
        });
      }
    });
  }
});
//获取名词解析接口——————————————————————————————————————————————————————————————————
router.get("/WordAnalysis", (req, res) => {
  console.log("获取新闻公告");
  // 查询角色表
  let sql = "";
  if (!req.query.name) {
    sql = `SELECT * from word_analysis ORDER BY id desc`;
  } else {
    sql = `SELECT * from word_analysis WHERE name LIKE '%${req.query.name}%' ORDER BY id desc`;
  }
  // 层级

  // 查找数据库第一层
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }

    // 转化
    result.some((item, index) => {
      // 转化时间
      item.Created_Data !== null
        ? (item.Created_Data = dayjs(item.Created_Data + 86400000).format(
            "YYYY-MM-DD"
          ))
        : "";
    });
    res.json({
      data: result,
      success: true,
    });
  });
});
//获取神代盘接口——————————————————————————————————————————————————————————————————
router.get("/GodGeneration", (req, res) => {
  console.log("获取神代盘");
  // 查询角色表
  let sql = "";
  if (!req.query.name) {
    sql = `SELECT * from god_generation ORDER BY id desc`;
  } else {
    sql = `SELECT * from god_generation WHERE name LIKE '%${req.query.name}%' OR color LIKE '%${req.query.name}%' OR description LIKE '%${req.query.name}%' ORDER BY id desc `;
  }
  // 层级

  // 查找数据库第一层
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }

    // 转化
    result.some((item, index) => {
      // 转化时间
      item.Created_Data !== null
        ? (item.Created_Data = dayjs(item.Created_Data + 86400000).format(
            "YYYY-MM-DD"
          ))
        : "";
    });
    res.json({
      data: result,
      success: true,
    });
  });
});
//获取wiki称号一览接口——————————————————————————————————————————————————————————————————
router.get("/TitleEdit", (req, res) => {
  console.log("获取称号一览");
  let row = [];
  // 查询角色表
  let sql = "";
  if (!req.query.page && !req.query.size) {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
  if (!req.query.name) {
    sql = `SELECT * from title_edit ORDER BY id desc`;
  } else {
    sql = `SELECT * from title_edit WHERE name LIKE '%${req.query.name}%' OR state LIKE '%${req.query.name}%' OR description LIKE '%${req.query.name}%' ORDER BY id desc`;
  }
  // 层级

  // 查找数据库第一层
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }

    // 转化
    result.some((item, index) => {
      // 转化时间
      item.Created_Data !== null
        ? (item.Created_Data = dayjs(item.Created_Data + 86400000).format(
            "YYYY-MM-DD"
          ))
        : "";
    });
    // 赋值合计页数
    Total = result.length;
    // 赋值非置顶数据
    row = result.splice(
      parseInt(req.query.page) * parseInt(req.query.size) -
        parseInt(req.query.size),
      parseInt(req.query.size)
    );
    res.send({
      data: row,
      Total: Total,
      success: true,
    });
  });
});
// 查看正式库封灭战详细接口————————————————————————————————————————————————————————————————————
router.get("/WarOfAnnihilation", (req, res) => {
  console.log("查看正式库封灭战详细接口");
  // 判断id参数是否有传参
  if (req.query.id === undefined || req.query.id === "") {
    res.send({
      msg: "未知错误",
      success: false,
    });
  } else {
    // 查数据库
    const sql = `SELECT * from war_of_annihilation WHERE id="${req.query.id}"`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      // 转化部分数组成js格式与时间矫正
      result.some((item) => {
        item.Login_rewards = JSON.parse(item.Login_rewards);
        item.Bonus_character = JSON.parse(item.Bonus_character);
        item.Bonus_mirror = JSON.parse(item.Bonus_mirror);
        item.Checkin_rewards = JSON.parse(item.Checkin_rewards);
        item.Signs_of_calamity = JSON.parse(item.Signs_of_calamity);
        item.Scattered_configuration = JSON.parse(item.Scattered_configuration);
        item.Strategy = JSON.parse(item.Strategy);
        item.Marketplace_Redemption1 = JSON.parse(item.Marketplace_Redemption1);
        item.Marketplace_Redemption2 = JSON.parse(item.Marketplace_Redemption2);
        item.Marketplace_Redemption3 = JSON.parse(item.Marketplace_Redemption3);
        item.Mission_rewards = JSON.parse(item.Mission_rewards);
        item.No_day_mission = JSON.parse(item.No_day_mission);
        item.Created_Data !== null
          ? dayjs(item.Created_Data + 86400000).format("YYYY-MM-DD")
          : "";
        // 活动开始时间——————————————————————————————————————————————————————————————
        if (item.Start_time !== null) {
          // 赋值开始时间
          item.Start_time = dayjs(item.Start_time + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
        // 活动结束时间——————————————————————————————————————————————————————
        if (item.End_time !== null) {
          // 赋值结束时间
          item.End_time = dayjs(item.End_time + 86400000).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
      });

      if (result.length === 0) {
        res.send({
          msg: "未查找到对应数据",
          success: false,
        });
      } else {
        res.json({
          data: result[0],
          success: true,
        });
      }
    });
  }
});

// 新增视频攻略合集——————————————————————————————————————————————————————————————————————————————————————
router.post("/NewVideoTips", (req, res) => {
  console.log("新增视频攻略合集");

  // 后端需要赋值的key字段，创建人、创建人id，创建时间
  let Keys = ["Created_Data", "Agree"];
  // 获取前端key字段并与后端新加字段合并转化成字符串
  let Key = Object.keys(req.body).concat(Keys).join(",");
  // 数据库赋值数据字段的字段
  let Values = "";
  // 循环前端传回来的数据valise
  Object.values(req.body).some((item) => {
    // 如果为空值则赋值null
    if (item === "") {
      Values = Values + "null,";
    } else if (typeof item === "object") {
      // 如果是数组则转化完后赋值
      Values = Values + "'" + JSON.stringify(item) + "'" + ",";
    } else {
      // 其余照常赋值
      Values = Values + "'" + item + "'" + ",";
    }
  });
  // 获取当前时间的拼接
  let DateItem = dayjs().format("YYYY-MM-DD");
  // 将后端要赋值的字段进行拼接，创建人、创建人id、创建时间、状态，id、是否同意
  let Valuess = `'${DateItem}','0'`;
  // 将前后端的合并
  Values = Values + Valuess;
  // 请求数据库
  const sql2 = `INSERT INTO capture(${Key}) VALUES(${Values}) `;
  db.query(sql2, function (err, rows) {
    if (err) {
      res.send({
        msg: "新增失败！",
        success: false,
      });
    } else {
      res.send({
        msg: "新增成功！",
        success: true,
      });
    }
  });
});
//获取视频攻略wiki接口——————————————————————————————————————————————————————————————————
router.get("/VideoStrategyWiki", (req, res) => {
  console.log("获取视频攻略wiki接口");
  let row = [];
  if (!req.query.page && !req.query.size) {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
  // 查询角色表
  let sql = "SELECT * from capture WHERE Agree=1 ORDER BY id desc";
  req.query.name
    ? (sql = `SELECT * from capture WHERE Agree=1 AND name LIKE '%${req.query.name}%' OR Agree=1 AND Created_By LIKE '%${req.query.name}%'  OR Agree=1 AND tab LIKE '%${req.query.name}%' OR Agree=1 AND remark LIKE '%${req.query.name}%' ORDER BY id desc`)
    : "";
  // 查找数据库第一层
  db.query(sql, function (err, result) {
    if (err) {
      return;
    }

    // 转化
    result.some((item, index) => {
      // 转化时间
      item.Created_Data !== null
        ? (item.Created_Data = dayjs(item.Created_Data + 86400000).format(
            "YYYY-MM-DD"
          ))
        : "";
      // 转化标签数据
      item.tab = JSON.parse(item.tab);
    });
    // 赋值合计页数
    Total = result.length;
    // 赋值非置顶数据
    row = result.splice(
      parseInt(req.query.page) * parseInt(req.query.size) -
        parseInt(req.query.size),
      parseInt(req.query.size)
    );
    res.send({
      data: row,
      Total: Total,
      success: true,
    });
  });
});

// 查看视频攻略wiki详细接口———————————————————————————————————————————————————————————————————
router.get("/VideoStrategyDetail", (req, res) => {
  console.log("查看视频攻略wiki详细接口");
  // 判断id参数是否有传参
  if (req.query.id === undefined || req.query.id === "") {
    res.send({
      msg: "未知错误",
      success: false,
    });
  } else {
    // 查数据库
    const sql = `SELECT * from capture WHERE id="${req.query.id}"`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      // 转化部分数组成js格式与时间矫正
      result.some((item) => {
        item.tab = JSON.parse(item.tab);
        let GetTime = new Date(item.Created_Data).getTime();
        item.Created_Data !== null
          ? (item.Created_Data = new Date(GetTime + 86400000))
          : "";
      });
      if (result.length === 0) {
        res.send({
          msg: "未查找到对应数据",
          success: false,
        });
      } else {
        res.json({
          data: result[0],
          success: true,
        });
      }
    });
  }
});
module.exports = router;
